package com.example.auto;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;

public class UserHandlerContext {
	private static final UserHandlerContext instance = new UserHandlerContext();
	private Handler handler;
	private Context context;
	
	public void toHome(int ms) {
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				backApp(0);
			}
		}, ms);
	}

	public void backApp(int delay) {
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				Intent intent = context.getPackageManager()
						.getLaunchIntentForPackage(context.getPackageName());
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				context.startActivity(intent);
			}
		}, 3 * 1000);
	}
	
	public Handler getHandler(){
		return this.handler;
	}
	
	public void init(Context context){
		this.handler = new Handler();
		this.context = context;
	}

	public static UserHandlerContext getInstance() {
		return instance;
	}
}
